'use server'

import { prisma } from '@/lib/prisma'
import { revalidatePath } from 'next/cache'

interface SupplierData {
  name: string
  contact: string
  phone: string
  address: string
  status: boolean
}

export async function createSupplier(data: SupplierData) {
  try {
    const supplier = await prisma.supplier.create({
      data
    })

    revalidatePath('/dashboard/suppliers')
    return { success: true, data: supplier }
  } catch (error) {
    console.error('创建供应商失败:', error)
    return { 
      success: false, 
      error: error instanceof Error ? error.message : '创建供应商失败'
    }
  }
}

export async function updateSupplier(id: number, data: SupplierData) {
  try {
    const supplier = await prisma.supplier.update({
      where: { id },
      data
    })

    revalidatePath('/dashboard/suppliers')
    return { success: true, data: supplier }
  } catch (error) {
    console.error('更新供应商失败:', error)
    return { 
      success: false, 
      error: error instanceof Error ? error.message : '更新供应商失败'
    }
  }
} 